package com.runda.controller.delivery;

import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bbjob.dto.Result;
import com.bbjob.model.SysDictionary;
import com.bbjob.model.SysOrganization;
import com.bbjob.model.User;
import com.runda.entity.JwtUser;
import com.runda.sec.SecContextUtil;
import com.runda.service.IVersionUpgradeService;
import com.runda.system.service.DicService;
import com.runda.system.service.MenuService;
import com.runda.system.service.OrganService;
import com.runda.system.service.UserService;
import com.runda.util.DataEntity;


@RestController
@RequestMapping("delivery/sys")
public class DeliverySysController {
	
	@Resource
	private OrganService organservice;
	@Resource
	private UserService userService;
	@Resource
	private DicService dicservice;
	@Resource
	private MenuService menuservice;
	@Resource
	private UserService userservice;
	@Resource
	private IVersionUpgradeService versionservice;
	/**
	 * 根据pid只获取一层结构
	 * @param pid
	 * @return
	 */
	@RequestMapping(value = "/organ/getOrgansByPid", method = RequestMethod.POST)
	@ResponseBody
	public Result getOrgansByPid(@RequestBody SysOrganization organ) {
		
		return new Result(organservice.getOrgansByPid(organ));
	}
	
	/**
	 * 根据pid只获取一层结构
	 * @param pid
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public Result getUserInfo(@RequestBody User user) {
		
		return new Result(userService.getUserInfo(user));
	}
	
	@RequestMapping(value = "/getDic", method = RequestMethod.GET)
	@ResponseBody
	public Result getDic(SysDictionary dic) {
		return new Result(dicservice.selectAll(dic));
	}
	
	@ApiOperation(value="获取试卷类型", notes="获取试卷类型")
	@RequestMapping(value="/getOperationAuth", method=RequestMethod.GET)
	public DataEntity getOperationAuth(Integer userId) {
		
		return DataEntity.getInstance(menuservice.getOperationAuth(userId));
	}
	
	@RequestMapping(value = "/getDifficulty", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getDifficulty() {
		return DataEntity.getInstance(dicservice.getDifficulty());
	}
	
	/**
	 * 用户修改密码
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "changePassword", method = RequestMethod.GET)
	@ResponseBody
	public Result changePassword(@RequestParam(required = true) Integer userid,@RequestParam(required = true) String oldpassword,@RequestParam(required = true) String newpassword) {
		int ok = userservice.changePassword(userid, oldpassword, newpassword);
		if(ok > 0) {
			return new Result(200, "操作成功");
		}else if(ok ==-1){
			return new Result(8106,"无法找到用户");
		}else if(ok == -2) {
			return new Result(8106,"密码不正确");
		}else {
			return new Result(8106,"操作失败");
		}
	}
	
	@RequestMapping(value = "getOrganInfo", method = RequestMethod.GET)
	@ResponseBody
	public Result getOrganInfo(String uniquecode) {
		
		return new Result(organservice.getOrganInfo(uniquecode));
	}
	
	@ApiOperation(value="查看升级(考试端)", notes="查看升级")
	@RequestMapping(value="checkUpgradeForExam", method=RequestMethod.GET)
	public DataEntity checkUpgradeForExam() {
		return DataEntity.getInstance(versionservice.checkUpgradeForExam());
	}
	
	@RequestMapping(value = "saveTeacher/{id}", method = RequestMethod.POST)
	@ResponseBody
	public Result saveTeacher(@RequestBody User user, @PathVariable("id") Integer id) {
		int ok = userservice.createUserByDirector(user, id);
		if(ok > 0) {
			return new Result(200, "保存成功");
		}else if(ok == -1) {
			return new Result(8106,"用户名已存在");
		}else {
			return new Result(8106,"保存失败");
		}
	}
	
	@RequestMapping(value = "editTeacher", method = RequestMethod.POST)
	@ResponseBody
	public Result editTeacher(@RequestBody User user) {
		int ok = userservice.editTeacher(user);
		if(ok > 0) {
			return new Result(200, "保存成功");
		}else if(ok == -1) {
			return new Result(8106,"用户名已存在");
		}else {
			return new Result(8106,"保存失败");
		}
	}
	
	@RequestMapping(value = "getTeacherList/{id}", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getTeacherList(User user, @RequestParam(required = false, defaultValue = "1") int page,
			@RequestParam(required = false, defaultValue = "10") int rows, @PathVariable("id") Integer id) {
		return DataEntity.getInstance(userservice.getTeacherList(user, page, rows,id));
	}
	
	@RequestMapping(value = "deleteTeacher/{userId}", method = RequestMethod.GET)
	@ResponseBody
	public Result deleteTeacher(Integer id, @PathVariable("userId") Integer userId) {
		int ok = userservice.deleteTeacher(id, userId);
		switch (ok) {
		case 0:
			return new Result(8106,"找不到用户");
		case 1:
			return new Result(200,"删除成功");
		case -1:
			return new Result(8106,"不能删除当前用户");
		case -2:
			return new Result(8106,"不能删除主任账户");
		case -3:
			return new Result(8106,"非法数据");
		default:
			return new Result(8106,"未知错误");
		}
	}
}
